package com.geekteam.geometrychess.handler.event;

import java.util.List;

import com.geekteam.geometrychess.database.DataAccessor;
import com.geekteam.geometrychess.extension.ChessExtension;
import com.geekteam.geometrychess.room.entity.Player;
import com.geekteam.geometrychess.utils.Code;
import com.geekteam.geometrychess.utils.Commands;
import com.geekteam.geometrychess.utils.Message;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

public class JoinZoneEventHandler extends BaseServerEventHandler{

	protected User mUser;
	protected Player mPlayer;
	private ChessExtension mExtension;
	
	@Override
	public void handleServerEvent(ISFSEvent event) throws SFSException {
		mExtension = (ChessExtension) getParentExtension();
		mUser = (User) event.getParameter(SFSEventParam.USER);
		mPlayer = DataAccessor.getInstance().getPlayerInfo(mUser);
		if(mPlayer!=null){
			try{
				//if extension contain player => disconnect both
				if(mExtension.containPlayer(mPlayer.mName)){
					trace("player existing...");
					Player p = mExtension.getPlayerByName(mPlayer.mName);
					disconnectUser(mUser);
					User user = getApi().getUserById(p.mSfsId);
					mExtension.sendPrivateError(Code.RELOAD_CLIENT_CODE, Message.RELOAD_CLIENT_DISCONNECT_OR_EXISTS_MESG, user.getId());
					disconnectUser(user);
					return;
				}else{
//					ISFSObject sfsObject = new SFSObject();
//					sfsObject = mPlayer.toSFSObjectSimple();
//					UserVariable userVariable = new SFSUserVariable(Key.VARIABLE, sfsObject);
//					userVariable.setHidden(false);
//					ArrayList<UserVariable> listUserVariables = new ArrayList<UserVariable>();
//					listUserVariables.add(userVariable);
//					ext.getApi().setUserVariables(mUser, listUserVariables);
					trace("add player sfsId: "+ mPlayer.mSfsId);
					mExtension.addPlayer(mPlayer);
					
					mExtension = (ChessExtension) this.getParentExtension();
					SFSObject numberObject = new SFSObject();
					List<String> groups = mExtension.getParentZone().getGroups();
					for(String group:groups){
						int numberUsers = mExtension.getParentZone().getUsersInGroup(group).size();
						trace("number player in zone: "+numberUsers);
						numberObject.putInt(group, numberUsers);
					}
					send(Commands.NUM_USERS_IN_GROUP, numberObject, mUser);
				}
			}catch(Exception ex){
				trace("join zone exception");
				mExtension.sendPrivateError(Code.RELOAD_CLIENT_CODE, Message.RELOAD_CLIENT_DISCONNECT_OR_EXISTS_MESG, mUser.getId());
				disconnectUser(mUser);
				return;
			}
		}else{
			SFSErrorData error = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
			throw new SFSException(Message.USER_NOT_FOUND, error);
		}
	}

	private void disconnectUser(User user) {
		try {
			getApi().disconnectUser(user);
		} catch (Exception e) { }
	}
}
